//
//  CJZYXCaKaQuanBu.m
//  MDDNew
//
//  Created by 孙昊坤 on 15/10/29.
//  Copyright © 2015年 heqing. All rights reserved.
//

#pragma mark -- 超级自由行查看全部（跳转）

#import "CJZYXCaKaQuanBu.h"

#import "TYDataManager.h"

#import "ZYXCell.h"

#import "UIImageView+WebCache.h"

// 下拉刷新
#import "MJRefresh.h"

#import "ChaoJiZhiYouXing.h"

// 宽高
#define Kheight [UIScreen mainScreen].bounds.size.height
#define Kwidth [UIScreen mainScreen].bounds.size.width

@interface CJZYXCaKaQuanBu ()<UITableViewDataSource, UITableViewDelegate>

// 建立数组保存解析出来的list数组
@property (nonatomic, strong) NSMutableArray *dataArray;

@property (nonatomic, strong) UITableView *tableView;

// 记录解析网址
@property (nonatomic, strong) NSString *url;

@property (nonatomic, assign) NSInteger i;
@property (nonatomic, assign) NSInteger hh;
// 设置boll判断下拉刷新的网址
@property (nonatomic, assign) BOOL zhuangTai;

@end

@implementation CJZYXCaKaQuanBu

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _i = 2;
    
    _hh = 2;
    
    _zhuangTai = YES;
    
    [self touBu];
    
    [self setUpData];
    
    [self addUITableView];
}

// 解析数据
- (void)setUpData
{
    NSString *string = [NSString stringWithFormat:@"http://open.qyer.com/qyer/discount/tickets_freewalker?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&id=%@", self.ids];
    NSString *string2 = [string stringByAppendingString:@"&lat=40.02931244757757&lon=116.3372840631773&order=2&page=1&product_type=1016%2C1018%2C1020&time=1&track_app_channel=App%2520Store&track_app_version=6.8&track_device_info=iPhone7%2C1&track_deviceid=3D292DEC-1F82-4FE7-9AE7-A1866F5F24E7&track_os=ios%25209.0.2&type=1&v=1"];
    
    _url = [NSString stringWithString:string];
    
    [[TYDataManager shareManager]requestDataWithUrl:string2 block:^(NSData *data){
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        self.dataArray = [NSMutableArray arrayWithArray:[[dic valueForKey:@"data"] valueForKey:@"list"]];
        
        // 下拉刷新
        self.tableView.footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            [self setUpDataWithUrl];
//            _jishu = _jishu + 1 ;
        }];
        
        [self.tableView reloadData];
        [self.tableView.footer endRefreshing];
    }];
}
// 刷新数据
- (void)setUpDataWithUrl
{
    if (_zhuangTai == YES) {
        NSString *string = [_url stringByAppendingFormat:@"&lat=40.02931244757757&lon=116.3372840631773&order=2&page=%ld", _i];
        NSString *string2 = [string stringByAppendingString:@"&product_type=1016%2C1018%2C1020&time=1&track_app_channel=App%2520Store&track_app_version=6.8&track_device_info=iPhone7%2C1&track_deviceid=3D292DEC-1F82-4FE7-9AE7-A1866F5F24E7&track_os=ios%25209.0.2&type=1&v=1"];
        
        
        [[TYDataManager shareManager]requestDataWithUrl:string2 block:^(NSData *data){
            
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            
            NSArray *array = [[dic valueForKey:@"data"] valueForKey:@"list"];
            
            for (int i = 0; i < array.count; i++) {
                [self.dataArray addObject:array[i]];
            }
            
            [self.tableView reloadData];
            [self.tableView.footer endRefreshing];
            
            _i = _i + 1;
        }];
    } else {
        // http://open.qyer.com/qyer/discount/local_discount?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&id=213&lat=40.02931244757757&lon=116.3372840631773&order=2&page=1&product_type=2410&time=1&track_app_channel=App%2520Store&track_app_version=6.8&track_device_info=iPhone7%2C1&track_deviceid=3D292DEC-1F82-4FE7-9AE7-A1866F5F24E7&track_os=ios%25209.0.2&type=1&v=1
        // 精彩当地游下拉刷新有bug数据一样
        NSString *string = [NSString stringWithFormat:@"http://open.qyer.com/qyer/discount/local_discount?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&id=%@",_ids];
        
        NSString *string2 = [string stringByAppendingFormat:@"&id=213&lat=40.02931244757757&lon=116.3372840631773&order=2&page=%ld", _hh];
        
        NSString *string3 = [string2 stringByAppendingString:@"&product_type=2410&time=1&track_app_channel=App%2520Store&track_app_version=6.8&track_device_info=iPhone7%2C1&track_deviceid=3D292DEC-1F82-4FE7-9AE7-A1866F5F24E7&track_os=ios%25209.0.2&type=1&v=1"];
        
        [[TYDataManager shareManager]requestDataWithUrl:string3 block:^(NSData *data){
            
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            
            NSArray *array = [[dic valueForKey:@"data"] valueForKey:@"list"];
            
            for (int i = 0; i < array.count; i++) {
                [self.dataArray addObject:array[i]];
            }
            
            [self.tableView reloadData];
            [self.tableView.footer endRefreshing];
            
            _i = _i + 1;
        }];
        
    }
    

}
// 布局

- (void)touBu
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 64, Kwidth, 50)];
    view.backgroundColor = [UIColor whiteColor];
    UIColor *color = [UIColor colorWithHue:146/255 saturation:146/255 brightness:146/255 alpha:0.5];
    //        _bianKuang.layer.borderColor = [color CGColor];
    view.layer.shadowOffset = CGSizeMake(0, 1);
    view.layer.shadowOpacity = 0.3;
    view.layer.shadowColor = [color CGColor];
    
    [self.view addSubview:view];
    
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button1 setTitle:@"超值自由行" forState:UIControlStateNormal];
    [button1 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    button1.frame = CGRectMake(0, 0, Kwidth / 2, 50);
    [button1 addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
    button1.tag = 1;
    [view addSubview:button1];
    
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button2 setTitle:@"精彩当地游" forState:UIControlStateNormal];
    [button2 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    button2.frame = CGRectMake(Kwidth / 2, 0, Kwidth / 2, 50);
    [button2 addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
    button2.tag = 2;
    [view addSubview:button2];
}

- (void)action:(UIButton *)button
{
    if (button.tag == 1) {
        [self setUpData];
        _zhuangTai = YES;
    }
    
    if (button.tag == 2) {
        _zhuangTai = NO;
        // &lat=40.02931244757757&lon=116.3372840631773&order=2&page=1&product_type=2410&time=1&track_app_channel=App%2520Store&track_app_version=6.8&track_device_info=iPhone7%2C1&track_deviceid=3D292DEC-1F82-4FE7-9AE7-A1866F5F24E7&track_os=ios%25209.0.2&type=1&v=1
        NSString *string = [NSString stringWithFormat:@"http://open.qyer.com/qyer/discount/local_discount?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&id=%@", self.ids];
        NSString *string2 = [string stringByAppendingString:@"&lat=40.02931244757757&lon=116.3372840631773&order=2&page=1&product_type=2410&time=1&track_app_channel=App%2520Store&track_app_version=6.8&track_device_info=iPhone7%2C1&track_deviceid=3D292DEC-1F82-4FE7-9AE7-A1866F5F24E7&track_os=ios%25209.0.2&type=1&v=1"];
        [self qieHuan:string2];
    }
}
// 点击切换数据
- (void)qieHuan:(NSString *)url
{
    [[TYDataManager shareManager]requestDataWithUrl:url block:^(NSData *data){
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        self.dataArray = [NSMutableArray arrayWithArray:[[dic valueForKey:@"data"] valueForKey:@"list"]];
        [self.tableView reloadData];
    }];
        
}


- (void)addUITableView
{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 115, Kwidth, Kheight - 100) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [self.view addSubview:_tableView];
}
// 代理方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *string = @"MyCell";
    ZYXCell *cell = [tableView dequeueReusableCellWithIdentifier:string];
    if (cell == nil) {
        cell = [[ZYXCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string];
    }
    
    cell.biaoTi.text = [_dataArray[indexPath.row]valueForKey:@"title"];
    [cell.tuPian sd_setImageWithURL:[_dataArray[indexPath.row]valueForKey:@"photo"]];
    cell.zheKou.text = [_dataArray[indexPath.row]valueForKey:@"priceoff"];
    cell.jiaGe.text = [_dataArray[indexPath.row]valueForKey:@"price"];
    
    // 加个
    NSMutableString *jiaGe = [NSMutableString stringWithString:[_dataArray[indexPath.row] valueForKey:@"price"]];
    NSRange range = NSMakeRange(0, 4);
    [jiaGe deleteCharactersInRange:range];
    
    NSString *str = [jiaGe stringByReplacingOccurrencesOfString:@"</em>" withString:@""];
    cell.jiaGe.text = str;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ChaoJiZhiYouXing *viewVC = [[ChaoJiZhiYouXing alloc] init];
    viewVC.ids = [_dataArray[indexPath.row]valueForKey:@"id"];;
    [self.navigationController pushViewController:viewVC animated:YES];
}

/*
 对应的接口 超值自由行 1段
 http://open.qyer.com/qyer/discount/tickets_freewalker?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&id=11&lat=40.02931244757757&lon=116.3372840631773&order=2&page=1&product_type=1016%2C1018%2C1020&time=1&track_app_channel=App%2520Store&track_app_version=6.8&track_device_info=iPhone7%2C1&track_deviceid=3D292DEC-1F82-4FE7-9AE7-A1866F5F24E7&track_os=ios%25209.0.2&type=1&v=1
 
 http://open.qyer.com/qyer/discount/tickets_freewalker?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&id=219&lat=40.02931244757757&lon=116.3372840631773&order=2&page=2&product_type=1016%2C1018%2C1020&time=1&track_app_channel=App%2520Store&track_app_version=6.8&track_device_info=iPhone7%2C1&track_deviceid=3D292DEC-1F82-4FE7-9AE7-A1866F5F24E7&track_os=ios%25209.0.2&type=1&v=1
 
 http://open.qyer.com/qyer/discount/tickets_freewalker?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&id=219&lat=40.02931244757757&lon=116.3372840631773&order=2&page=3&product_type=1016%2C1018%2C1020&time=1&track_app_channel=App%2520Store&track_app_version=6.8&track_device_info=iPhone7%2C1&track_deviceid=3D292DEC-1F82-4FE7-9AE7-A1866F5F24E7&track_os=ios%25209.0.2&type=1&v=1
 
 精彩当地游
 http://open.qyer.com/qyer/discount/local_discount?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&id=213&lat=40.02931244757757&lon=116.3372840631773&order=2&page=1&product_type=2410&time=1&track_app_channel=App%2520Store&track_app_version=6.8&track_device_info=iPhone7%2C1&track_deviceid=3D292DEC-1F82-4FE7-9AE7-A1866F5F24E7&track_os=ios%25209.0.2&type=1&v=1
 
 */

@end
